﻿/*
 * Created by SharpDevelop.
 * User: Dhanesh
 * Date: 02-10-2008
 * Time: 00:06:35
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

namespace ChallanSystem
{
	/// <summary>
	/// Description of Customers.
	/// </summary>
	public partial class Customers : Form
	{
		Customer cust = null;
		DataSet ds = null;
		DataRow dr = null;
		DataTable dtFiltered = null;
		
		public Customers()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void BtnNewClick(object sender, EventArgs e)
		{
			ClearControls();
			enableAllControls();
			btnSave.Enabled = true;
			btnCancel.Enabled = true;
			btnNew.Enabled = false;
			btnUpdate.Enabled = false;
			btnDelete.Enabled = false;
		}
		
		void enableAllControls()
		{
			txtPinCode.Enabled = true;
			textBox2.Enabled = true;
			textBox3.Enabled = true;
			textBox4.Enabled = true;
			txtCustomerName.Enabled = true;
			txtEmail.Enabled = true;
			txtFax.Enabled = true;
			txtPhone1.Enabled = true;
			txtPhone2.Enabled = true;
			//txtSearch.Enabled = true;
		}

		void disableAllControls()
		{
			
			txtPinCode.Enabled = false;
			textBox2.Enabled = false;
			textBox3.Enabled = false;
			textBox4.Enabled = false;
			txtCustomerName.Enabled = false;
			txtEmail.Enabled = false;
			txtFax.Enabled = false;
			txtPhone1.Enabled = false;
			txtPhone2.Enabled = false;
			//txtSearch.Enabled = false;

		}

		
		void BtnSaveClick(object sender, EventArgs e)
		{
			cust = new Customer();
			if(validate())
			{
				if(isNumericCheck())
				{
					cust.Name = txtCustomerName.Text;
					cust.Address1 = textBox4.Text != ""?textBox4.Text:" ";
					cust.Address2 = textBox3.Text != ""?textBox3.Text:" ";
					cust.Address3 = textBox2.Text != ""?textBox2.Text:" ";
					cust.Postcode = txtPinCode.Text != ""?txtPinCode.Text:" ";
					cust.Phone1 = txtPhone1.Text != ""?txtPhone1.Text:" ";
					cust.Phone2 = txtPhone2.Text != ""?txtPhone2.Text:" ";
					cust.Fax = txtFax.Text != ""?txtFax.Text:" ";
					cust.Email = txtEmail.Text != ""?txtEmail.Text:" ";
					
					if(rdoRetail.Checked)
					{
						cust.Customer_Type = 1;
					}else if(rdoSemiRetail.Checked)
					{
						cust.Customer_Type = 2;
					}else if(rdoWholesale.Checked)
					{
						cust.Customer_Type = 3;
					}else if(rdoSemiWholeSale.Checked)
					{
						cust.Customer_Type = 4;
					}
					
					bool ret = cust.Add();
					if(ret == true)
					{
						MessageBox.Show("Data added Successfully!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
						btnSave.Enabled = false;
						btnCancel.Enabled = false;
						btnNew.Enabled = true;
						btnUpdate.Enabled = true;
						btnDelete.Enabled = true;
						ClearControls();
						RefreshDataGrid();
					}
				}
				else
					MessageBox.Show("Please Enter Valid Data !!!");
			}
			else
			{
				MessageBox.Show("All (*) Fields Are Mandatory !!!");
			}
		}
		
		void ClearControls()
		{
			txtCustomerName.Text = String.Empty;
			textBox4.Text = String.Empty;
			textBox3.Text = String.Empty;
			textBox2.Text = String.Empty;
			txtPinCode.Text = String.Empty;
			txtPhone1.Text = String.Empty;
			txtPhone2.Text = String.Empty;
			txtFax.Text = String.Empty;
			txtEmail.Text = String.Empty;
		}
		
		void CustomersLoad(object sender, EventArgs e)
		{
			disableAllControls();
			RefreshDataGrid();
		}
		
		void BtnCancelClick(object sender, EventArgs e)
		{
			ClearControls();
			
			disableAllControls();
			
			btnNew.Enabled = true;
			btnUpdate.Enabled = true;
			btnDelete.Enabled = true;
			btnSave.Enabled = false;
			//btnSave.ForeColor = System.Drawing.Color.Gray;
			btnCancel.Enabled = false;
			//btnCancel.ForeColor = System.Drawing.Color.Gray;
			RefreshDataGrid();
		}
		
		void BtnDeleteClick(object sender, EventArgs e)
		{
			if(dr != null)
			{
				int id = Convert.ToInt32(dr["Customer_ID"].ToString());
				cust = new Customer();
				
				bool ret = cust.Delete(id);
				if(ret == true)
				{
					MessageBox.Show("Data deleted successfully!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
					btnSave.Enabled = false;
					btnCancel.Enabled = false;
					btnNew.Enabled = true;
					ClearControls();
					RefreshDataGrid();
				}
			}
			else
			{
				MessageBox.Show("No row selected to update.");
			}
		}
		
		void RefreshDataGrid()
		{
			cust = new Customer();
			ds = cust.View();
			datagridCustomers.SetDataBinding(ds, "Customers");
		}
		
		
		void BtnUpdatebtnUpdateClick(object sender, EventArgs e)
		{
			if(dr != null)
			{
				if(validate())
				{
					int id = Convert.ToInt32(dr["Customer_ID"].ToString());
					cust = new Customer();
					cust.Name = txtCustomerName.Text;
					cust.Address1 = textBox4.Text != ""?textBox4.Text:"";
					cust.Address2 = textBox3.Text != ""?textBox3.Text:"";
					cust.Address3 = textBox2.Text != ""?textBox2.Text:"";
					cust.Postcode = txtPinCode.Text != ""?txtPinCode.Text:"";
					cust.Phone1 = txtPhone1.Text != ""?txtPhone1.Text:"";
					cust.Phone2 = txtPhone2.Text != ""?txtPhone2.Text:"";
					cust.Fax = txtFax.Text != ""?txtFax.Text:"";
					cust.Email = txtEmail.Text != ""?txtEmail.Text:"";
					
					if(rdoRetail.Checked)
					{
						cust.Customer_Type = 1;
					}else if(rdoSemiRetail.Checked)
					{
						cust.Customer_Type = 2;
					}else if(rdoWholesale.Checked)
					{
						cust.Customer_Type = 3;
					}else if(rdoSemiWholeSale.Checked)
					{
						cust.Customer_Type = 4;
					}
					
					bool ret = cust.Update(id);
					if(ret == true)
					{
						MessageBox.Show("Data updated successfully!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
						btnSave.Enabled = false;
						btnCancel.Enabled = false;
						btnNew.Enabled = true;
						ClearControls();
						RefreshDataGrid();
					}
				}
				else
				{
					MessageBox.Show("No row selected to update.");
				}
			}
		}
		
		void DatagridCustomersNavigate(object sender, NavigateEventArgs ne)
		{
			
		}
		
		void DatagridCustomersClick(object sender, EventArgs e)
		{
			enableAllControls();
			PopulateFields();
		}
		
		
		void DatagridCustomersCurrentCellChanged(object sender, EventArgs e)
		{
			enableAllControls();
			PopulateFields();
		}
		
		void PopulateFields()
		{
			try
			{
				if(datagridCustomers.CurrentRowIndex > -1 && dtFiltered == null)
				{
					dr = ds.Tables[0].DefaultView[datagridCustomers.CurrentRowIndex].Row;
					txtCustomerName.Text = dr["Customer_Name"].ToString();
					textBox4.Text = dr["Address1"].ToString();
					textBox3.Text = dr["Address2"].ToString();
					textBox2.Text = dr["Address3"].ToString();
					txtPinCode.Text = dr["Postcode"].ToString();
					txtPhone1.Text = dr["Phone1"].ToString();
					txtPhone2.Text = dr["Phone2"].ToString();
					txtFax.Text = dr["Fax"].ToString();
					txtEmail.Text = dr["Email"].ToString();
					switch(Convert.ToInt32(dr["Cust_Typ_Lkp_ID"].ToString()))
					{
						case 1:
							rdoRetail.Checked = true;
							break;
						case 2:
							rdoSemiRetail.Checked = true;
							break;
						case 3:
							rdoWholesale.Checked = true;
							break;
						case 4:
							rdoSemiWholeSale.Checked = true;
							break;
						default:
							break;
					}
				}
				else
				{
					dr = dtFiltered.DefaultView[datagridCustomers.CurrentRowIndex].Row;
					txtCustomerName.Text = dr["Customer_Name"].ToString();
					textBox4.Text = dr["Address1"].ToString();
					textBox3.Text = dr["Address2"].ToString();
					textBox2.Text = dr["Address3"].ToString();
					txtPinCode.Text = dr["Postcode"].ToString();
					txtPhone1.Text = dr["Phone1"].ToString();
					txtPhone2.Text = dr["Phone2"].ToString();
					txtFax.Text = dr["Fax"].ToString();
					txtEmail.Text = dr["Email"].ToString();
					switch(Convert.ToInt32(dr["Cust_Typ_Lkp_ID"].ToString()))
					{
						case 1:
							rdoRetail.Checked = true;
							break;
						case 2:
							rdoSemiRetail.Checked = true;
							break;
						case 3:
							rdoWholesale.Checked = true;
							break;
						case 4:
							rdoSemiWholeSale.Checked = true;
							break;
						default:
							break;
					}
				}
			}
			catch(IndexOutOfRangeException e)
			{
				MessageBox.Show("Exception : "+e);
			}
		}
		
		bool validate()
		{
			bool validate = true;
			
			if(txtPinCode.Text == "" || txtCustomerName.Text == "" || txtPhone1.Text == "" || textBox4.Text == "")
			{
				if(!rdoRetail.Checked)
				{
					validate = false;
				}
				else if(!rdoSemiRetail.Checked)
					validate = false;
				else if(!rdoSemiWholeSale.Checked )
					validate = false;				
				else if(!rdoWholesale.Checked)
					validate = false;				
			}
			else
				validate = true;
			
			return validate;
		}
		bool isNumericCheck()
		{
			bool validate = true;
			
			try
			{
				uint intValue = Convert.ToUInt32(txtPhone1.Text);
				//MessageBox.Show("After 1st Validation : "+intValue.ToString());
				uint intValue3 = Convert.ToUInt32(txtPinCode.Text);
				//MessageBox.Show("After 2nd Validation : "+intValue3.ToString());
			}
			catch(Exception)
			{
				validate = false;
			}
			return validate;
		}
		
		
		void Button1Click(object sender, EventArgs e)
		{
			Util searchUtil = new Util();
			string filter = string.Empty;
			filter = txtSearch.Text;
			string columnName = string.Empty;
			columnName = "Customer_Name";
			//MessageBox.Show("Filter B4 : "+filter);
//			filter = "Customer_Name = '"+ds.Tables[0].DefaultView[0].Row["Customer_Name"].ToString()+"'";
			//MessageBox.Show("Filter After : "+filter);
			dtFiltered = searchUtil.Search(ds.Tables[0], filter, columnName);
//			ds.Tables.RemoveAt(0);
//			ds.Tables.Add(dtFiltered);
			datagridCustomers.DataSource = dtFiltered;
		}
	}
}
